package com.qf.fmall.service.impl;

import com.qf.fmall.dao.UsersMapper;
import com.qf.fmall.entity.Users;
import com.qf.fmall.service.UserService;
import com.qf.fmall.utils.JWTUtils;
import com.qf.fmall.utils.MD5Utils;
import com.qf.fmall.vo.ResStatus;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 王权富贵
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private UsersMapper usersMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo getUserByUserName(String username, String password) {
        Users users = new Users();
        users.setUsername(username);
        Users user = usersMapper.selectOne(users);
        ResultVo resultVo = new ResultVo();
        if (Objects.isNull(user)) {
            resultVo.setMsg("用户不存在");
            resultVo.setCode(ResStatus.NO);
            resultVo.setData(null);
            return resultVo;
        }
        String pwd = user.getPassword();
        String md5 = MD5Utils.md5(password);
        if (pwd.equals(md5)) {
            resultVo.setMsg("登陆成功");
            /*
             * 生成token字符串(起到令牌的作用）
             * 指定生成字字符串的规则,通过Base64格式话
             * 在前端页面中将传递过去的msg存储进cookie
             * */

            String token = JWTUtils.createToken(user.getUsername(), user.getUserId().toString(), "qf6666");
            resultVo.setCode(ResStatus.OK);
            resultVo.setMsg(token);
            resultVo.setData(user);
            return resultVo;
        }
        return resultVo;
    }

    @Override
    public ResultVo insertUsers(Users users) {
        ResultVo resultVo = new ResultVo();
        if (users == null) {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("注册失败");
            return resultVo;
        }
        Example example = new Example(Users.class);
        example.createCriteria()
                .andEqualTo("username", users.getUsername());
        List<Users> list = usersMapper.selectByExample(example);
        if (null != list && list.size() != 0) {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("用户已存在,请重新输入");
            return resultVo;
        }
        String password = users.getPassword();
        users.setPassword(MD5Utils.md5(password));
        users.setUserImg("img/a.jpg");
        users.setUserRegtime(new Date());
        users.setUserModtime(new Date());
        int insert = usersMapper.insert(users);
        if ((insert > 0)) {
            resultVo.setCode(ResStatus.OK);
            resultVo.setMsg("注册成功");
            return resultVo;
        } else {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("注册失败");
            return resultVo;
        }
    }
}
